<?php
class Admin_Page extends Admin_Controller{
	function __construct(){
		parent::__construct();
		$this->check_permissions();
		$_SESSION['module'] = "ck_".$this->module;
		$this->load->model('page_m');
	}
	
	public function index(){
		$page_list = $this->page_m->page_list();//get_all();
		$model = array();
		//chuyen de quy thanh array cap 1
		array_level1($page_list,$model);
		$m = array();
		foreach($model as $item){
			if(count($item) > 0){
				$m[] = $item;
			}
		}		
		$this->template
			->set('action',$this->action)
			->set('model',$m)
			->set_breadcrumb(lang('page:page'))
			->build($this->controller.'/'.$this->method);
	}
	
	public function create(){
		$model = $this->input->post();
		$this->form_validation();		
		$type =  substr($this->input->post('btnAction'), 0);
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				//print_r($model);exit;
				$skip = array('btnAction');
				foreach($skip as $obj){
				if(array_key_exists($obj,$model)){
					unset($model[$obj]); // bo nh?ng bi?n post wa ko ph?i d? li?u
					}
				}
				$id_insert = $this->page_m->insert($model);
				$this->redirect($this->method,$type,$id_insert);
			}
		}
		
		$page_list = $this->page_m->page_list();
		
		$list = array();
		//chuyen de quy thanh array cap 1
		array_level1($page_list,$list);
		$select = array();
		foreach($list as $item){
			if(count($item) > 0){
				$select[] = $item;
			}
		}
		
		$page_select = array_for_select($select,'id','title','frefix');
		
		$model = array('model'=>$model,'page_select'=>$page_select);
		$this->view($model);
	}
	
	public function edit($id){
		$model = (array)$this->page_m->get($id);
		$this->form_validation();
		$type =  substr($this->input->post('btnAction'), 0);
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				$model = $this->input->post();
				//print_r($model);exit;
				$skip = array('btnAction','id');
				foreach($skip as $obj){
				if(array_key_exists($obj,$model)){
					unset($model[$obj]); // bo nh?ng bi?n post wa ko ph?i d? li?u
					}
				}
				$this->page_m->update($id,$model);
				$this->redirect($this->method,$type,$id);
				/*if($type == 'save')
					redirect('admin/'.$this->module);
				if($type == 'add')
					redirect('admin/'.$this->module.'/'.$this->method);
				if($type == 'edit')
					redirect('admin/'.$this->module.'/edit/'.$id);*/
				
			}
		}
		
		$page_list = $this->page_m->page_list();
		
		$list = array();
		//chuyen de quy thanh array cap 1
		array_level1($page_list,$list);
		$select = array();
		foreach($list as $item){
			if(count($item) > 0){
				$select[] = $item;
			}
		}
		
		$page_select = array_for_select($select,'id','title','frefix',$id);//frefix
		
		$model = array('model'=>$model,'page_select'=>$page_select);
		$this->view($model);
	}
	
	//delete
	public function delete($id){
		$this->page_m->delete($id);
		redirect('admin/page');
	}
	
	function ordering(){
		$model = $this->input->post();
		$id = $model['id'];
		unset($model['id']);
		$this->page_m->ordering($id,$model);
	}
	
	function form_validation(){
		$this->form_validation->set_rules('title',lang('news:title'),'required|max_length[50]');
		$this->form_validation->set_rules('slug',lang('global:slug'),'required|max_length[50]');
		$this->form_validation->set_rules('class','Class','max_length[20]');
	}
	
	/*function view($data){
		$this->template
			->set($data)
			->append_js('jquery.tabify.js')
			->build($this->controller.'/form');
	}
	
	function redirect($method,$type,$id){
		if($type == 'save')
			redirect('admin/'.$this->module);
		if($type == 'add')
			redirect('admin/'.$this->module.'/'.$method);
		if($type == 'edit')
			redirect('admin/'.$this->module.'/edit/'.$id);
	}*/
}